//
//  Client_RF225_StkInNController.swift
//  Wmsmobile
//
//  Created by xuNick on 16/9/9.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class Client_RF225_StkInNController: HDFounctionBaseController , HDBarcodeInputCellInputDelegate , HDFounctionBottomViewDelegate , HDDatePickerCellDelegate , HDNumberInputCellDelegate{
    //MARK: - property
    let dataArray:[[String]] = [
        ["预检单号"],
        ["托盘条码"],
        ["商品条码","商品名称","码盘","单位/重量","保质期","验收标准","应收/已收"],
        ["生产日期","批号/规格", "整件/零散"]
    ]
    
    var  piOrderNum: String?
    var piPalletCode: String?
    var dateInProduce: NSDate?
    var piLot : String?
    var endFlag : Bool = false
    var emptyFlag = true
    lazy private var bottomView:HDFounctionBottomView = {
        let view = HDFounctionBottomView(array: ["结束收货", "托盘查询" ,"暂停收货" , "托盘重收"])
        return view
    }()
    
    //MARK: - lifeCycle
    override func viewDidLoad() {
        self.tableView.registerClass(HDBarcodeInputCell.self, forCellReuseIdentifier: "HDBarcodeInputCell")
        self.tableView.registerClass(HDProductInfocell.self, forCellReuseIdentifier: "HDProductInfocell")
        self.tableView.registerClass(HDDatePickerCell.self, forCellReuseIdentifier: "HDDatePickerCell")
        self.tableView.registerClass(HDNumberInputCell.self, forCellReuseIdentifier: "HDNumberInputCell")
        self.tableView.addSubview(self.bottomView)
        self.tableView.contentInset.bottom = bottomView.height
        self.bottomView.delegate = self
        super.viewDidLoad()
    }
    
    override func viewDidAppear(animated: Bool) {
        let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! HDBarcodeInputCell
        cell.state = inputViewState.activated
    }
    //MARK: - delegate
    func finishNumInput(cell: HDNumberInputCell) {
        if cell.titleView.text == "批号/规格"{
            if Int(cell.boxNumberTextField.text ?? "0") == nil{
                self.showMessage("输入的批号不合法", success: false)
                return
            }
        }
        if cell.titleView.text == "整件/零散" {
            if Int(cell.boxNumberTextField.text ?? "0") == nil{
                self.showMessage("输入的整件数不合法", success: false)
                return
            }
            if Double(cell.bagNumberTextField.text ?? "0.0") == nil{
                self.showMessage("输入的零散数不合法", success: false)
                return
            }
        }
        let indexPath = self.tableView.indexPathForCell(cell)
        switch indexPath!.row {
        case 1:
            self.goNextCell(cell, nextInsexPath: NSIndexPath(forItem: 2, inSection: 3))
            piLot = cell.boxNumberTextField.text
        case 2:
            let formeter = NSDateFormatter()
            formeter.dateFormat = "yyyy-MM-dd"
            self.getData(["piOrderNum"  : self.piOrderNum! , "piStkinNum" : self.downloadData["poStkinNum"]! , "piPalletCode" : self.piPalletCode! , "piArticle" : self.downloadData["内码"]! , "piQpc" : (cell.boxNumberTextField.text ?? "") + "$$" + (cell.bagNumberTextField.text ?? "") , "piProDate" : formeter.stringFromDate(dateInProduce!) , "piLot" : piLot! , "piWeight" : "0" ,"piCls" : "普通收货" , "piUnloader" : "0","piStkInTem" : "0" ,"piVendorLots" : "" ,"piFinishPallet" : "1" , "piOperator" : (userInfo?.userName)!], bodyMethod: "updStkin", suffix: nil, success: { (dict) in
                self.reuseFlag = false
                self.tableView.reloadData()
                self.goNextCell(cell, nextInsexPath: NSIndexPath(forItem: 0, inSection: 1))
                let firstcell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! HDBarcodeInputCell
                self.showMessage("收货成功", success: true)
                self.endFlag = true
                firstcell.codeInputView.textField.text = self.piOrderNum!
                self.reuseFlag = true
                }, failed: { dict in
                   
            })
        default:
            break
        }
    }
    
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        self.bottomView.y = KscreenHeight - bottomView.height + self.tableView.contentOffset.y
    }
    func finishDatePick(date: NSDate) {
        let dateInProduced = date
        dateInProduce = date
        let formeter = NSDateFormatter()
        formeter.dateFormat = "yyyy-MM-dd"
        let formeterDate = NSDateFormatter()
        formeterDate.dateFormat = "yyyyMMdd"
        guard let _ = self.downloadData["poValidPeriod"] else{
            return
        }
//        let dateCell = (self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 3)) as! HDInputViewStateProtocol)
        let dateCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 3)) as! HDInputViewStateProtocol
        let qualityDate = (self.downloadData["poValidPeriod"]! as NSString).doubleValue
        let remainDay = qualityDate - (NSDate().timeIntervalSinceDate(dateInProduced) / 86400.0) //剩余保质期
        if remainDay > (self.downloadData["poStkLMTDays"]! as NSString).doubleValue{//拿来和收货控制天数比较
            self.getData(["piArticle" : self.downloadData["内码"]! , "piDate" : formeter.stringFromDate(dateInProduced)], bodyMethod: "chkDateIsLatest", suffix: nil, success: { (dict) in
                self.goNextCell(dateCell, nextInsexPath: NSIndexPath(forItem: 1, inSection: 3))
                let numberCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 1, inSection: 3)) as! HDNumberInputCell
                numberCell.boxNumberTextField.text = formeterDate.stringFromDate(date)
                numberCell.bagNumberTextField.text = self.downloadData["poSpec"] ?? ""
                }, failed: {_ in
                    let alertController = UIAlertController(title: "提示", message: "确定使用该日期", preferredStyle: UIAlertControllerStyle.Alert)
                    alertController.addAction(UIAlertAction(title: "是", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
                        self.goNextCell(self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 3)) as! HDInputViewStateProtocol, nextInsexPath: NSIndexPath(forItem: 1, inSection: 3))
                    }))
                    alertController.addAction(UIAlertAction(title: "否", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
                        dateCell.state = inputViewState.wrong
                        self.showMessage("重新选择日期", success: false)
                    }))
                    self.presentViewController(alertController, animated: true, completion: nil)
            })
        }else{
            self.showMessage("请重新输入日期", success: false)
            dateCell.state = inputViewState.wrong
            return
        }

    }
    func clickFounctionBottom(position: NSInteger) {
        if position == 1{
        }
    
        switch position {
        case 0:
            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forItem: 0,inSection: 0) , atScrollPosition: UITableViewScrollPosition.Top, animated: false)
            let piOrdNumCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! HDBarcodeInputCell
            let celltext = piOrdNumCell.codeInputView.textField.text
            if celltext?.characters.count < 1{
                self.showMessage("请输入预检单号", success: false)
                return
            }
            self.getData(["piOrdNum" : piOrderNum ??  piOrdNumCell.codeInputView.textField.text!, "piOperator" : (userInfo?.userName)! , "piAction" : "1"], bodyMethod: "finishAllStkin", suffix: "330", success: { (dict) in
                 self.showMessage("结束收货成功", success: true)
                self.reuseFlag = false
                self.tableView.reloadData()
                self.goNextCell(self.activedCell ?? piOrdNumCell, nextInsexPath: NSIndexPath(forItem: 0, inSection: 0))
                }, failed: { dict in
                    if dict["finishAllStkinResult"] == "1" {
                        let alertVC = UIAlertController(title: "提示", message: dict["poErrMsg"], preferredStyle: UIAlertControllerStyle.Alert)
                        alertVC.addAction(UIAlertAction(title: "否", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) in
                        }))
                        alertVC.addAction(UIAlertAction(title: "是", style: UIAlertActionStyle.Default, handler: { [weak self] (UIAlertAction) in
                            if self != nil {
                                self!.getData(["piOrdNum" : self!.piOrderNum ??  piOrdNumCell.codeInputView.textField.text!, "piOperator" : (userInfo?.userName)! , "piAction" : "0"], bodyMethod: "finishAllStkin", suffix: "330", success: { (dict) in
                                    self!.showMessage("结束收货成功", success: true)
                                    self?.resetController(self!.activedCell ?? piOrdNumCell, nextItem: 0, nextSection: 0, message: nil, success: true)
                                    self?.emptyFlag = true
                                    }, failed: { (dict) in
                                })
                            }
                        }))
                        self.presentViewController(alertVC, animated: true, completion: nil)
                    }
            })
           break
        case 1:
            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 1), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
            let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 1)) as! HDBarcodeInputCell
            if cell.codeInputView.textField.text?.characters.count > 0 {
                self.getData(["piPalletCode" : cell.codeInputView.textField.text!], bodyMethod: "qryPallet", suffix: nil, success: { (dict) in
                    let infoview = infoView(dict: dict, piPalletCode: cell.codeInputView.textField.text!)
                    self.navigationController?.view.addSubview(infoview)
                    }, failed: { _ in
                })
                
            }else{
                self.showMessage("请输入托盘条码", success: true)
            }
            
            return
        case 2:
            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0) , atScrollPosition: UITableViewScrollPosition.Top, animated: false)
            let firstCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! HDBarcodeInputCell
            if firstCell.state != inputViewState.disabled {
                self.showMessage("验证预检单号后再试", success: false)
                return
            }
            self.getData(["piStkinNum" : self.downloadData["poStkinNum"] ?? "" , "piOperator" : (userInfo?.userName)!], bodyMethod: "finishStkin", suffix: "330", success: { (dict) in
                self.showMessage("暂停收货成功", success: true)
                let alertVC = UIAlertController(title: "提示", message: "暂停收货成功，要收另外的预检单吗？", preferredStyle: UIAlertControllerStyle.Alert)
                alertVC.addAction(UIAlertAction(title: "否", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
                    self.navigationController?.popViewControllerAnimated(true)
                }))
                alertVC.addAction(UIAlertAction(title: "是", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
                    self.resetController(self.activedCell ?? firstCell, nextItem: 0, nextSection: 0, message: nil, success: true)
                    self.emptyFlag = true
                }))
                self.presentViewController(alertVC, animated: true, completion: nil)
                }, failed: { dict in
                    
            })
            break
        case 3:
            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forItem: 0 , inSection: 1), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
            let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 1)) as! HDBarcodeInputCell
            guard cell.codeInputView.textField.text?.characters.count > 0 else{
                self.showMessage("请输入托盘条码", success: false)
                return
            }
            
            self.showAlert("提示", message: "确认要重收吗", actionLTitle: "否", actionRTitle: "是", clickLeft: { 
                
                }, clickRight: { 
                    let cellText = cell.codeInputView.textField.text!
                    self.getData(["piPalletCode" : cellText , "piFlag" : "3" ,"piUser" : (userInfo?.userName)! , "piStkIn" : self.downloadData["poStkinNum"] ?? ""], bodyMethod: "handlePallet", suffix: nil, success: { (dict) in
                        self.piPalletCode = cellText
                        self.resetController(self.activedCell!, nextItem: 0, nextSection: 1, message: "托盘重收成功", success: true)
                        self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
                        let cellF = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! HDBarcodeInputCell
                        cellF.codeInputView.textField.text = self.piOrderNum
                        }, failed: { _ in //错误的种类
                            cell.state = inputViewState.wrong
                    })
            })
            
            break
        default:
            break
        }
    }
    func clicKReturn(cell: HDBarcodeInputCell) {
        let indexpath = self.tableView.indexPathForCell(cell)!
        switch indexpath.section * 100 + indexpath.row  {
        case 0: //第一个cell回车
            guard cell.codeInputView.textField.text?.characters.count > 0 else{
                self.showMessage("请输入预检单号", success: false)
                return
            }
//               NetworkTool.sharedTools.postSoap(["piOrdNum" : cell.codeInputView.textField.text!], bodyMethod: "chkOrd", suffix: nil, success:
            let cellText = cell.codeInputView.textField.text!
          self.getData(["piOrdNum" : cellText], bodyMethod: "chkOrd", suffix: nil, success: { (dict) in
            self.getData(["piOrdNum" : cellText , "piOperator" : (userInfo?.userName)! , "piCls" : "普通收货" , "piUnloader" : "0"], bodyMethod: "startStkin", suffix: nil, success: { (dict) in
                self.emptyFlag = false
                self.piOrderNum = cellText
                self.goNextCell(cell, nextInsexPath: NSIndexPath(forItem: 0, inSection: 1))
                }, failed: { _ in
                    cell.state = inputViewState.wrong
            })
            }, failed: { _ in
                cell.state = inputViewState.wrong
          })
        case 100:
            guard cell.codeInputView.textField.text?.characters.count > 0 else{
                self.showMessage("请输入托盘条码", success: false)
                return
            }
            let cellText = cell.codeInputView.textField.text!
            self.getData(["piPalletCode" : cellText , "piFlag" : "1" ,"piUser" : (userInfo?.userName)! , "piStkIn" : self.downloadData["poStkinNum"] ?? ""], bodyMethod: "handlePallet", suffix: nil, success: { (dict) in
                self.piPalletCode = cellText
                self.goNextCell(cell, nextInsexPath: NSIndexPath(forItem: 0, inSection: 2))
                }, failed: { _ in
                    cell.state = inputViewState.wrong
            })
        case 200:
            guard cell.codeInputView.textField.text?.characters.count > 0 else{
                self.showMessage("请输入商品条码", success: false)
                return
            }
            let cellText = cell.codeInputView.textField.text!
            self.oneToManyHandler(cellText, piOwner: self.downloadData["poOwner"] ?? "", piBin: nil, success: { (dict) in
                self.downloadData += dict
                self.getData(["piOrderNum" : self.piOrderNum! , "piStkinNum" : self.downloadData["poStkinNum"] ?? "" , "piPalletCode" : self.piPalletCode! , "piOperator" : (userInfo?.userName)! , "piArticle" : self.downloadData["内码"] ?? "" , "piCls" : "普通收货"], bodyMethod: "chkArticle", suffix: nil, success: { (dict) in
                    for i in 1...6 {//展示需要显示的信息
                        let produtionCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: i, inSection: 2)) as! HDProductInfocell
                        switch i {
                        case 1:
                            produtionCell.textField.text = self.downloadData["poArticleName"]
                        case 2:
                            produtionCell.textField.text = self.downloadData["poPaqStandard"]!
                        case 3:
                            produtionCell.textField.text = self.downloadData["poMunit"]! + "/" + self.downloadData["poWeight"]!
                        case 4:
                            let string = self.downloadData["poValidPeriod"]
                            let array = string!.componentsSeparatedByString(" ")
                            produtionCell.textField.text = array.first
                        case 5:
                            produtionCell.textField.text = self.downloadData["poDateChkStd"]
                        case 6:
                            produtionCell.textField.text = self.downloadData["poQpc"]! + "/" + self.downloadData["poRcvQpc"]!
                        default:
                            break
                        }
                    }
                    let dateCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 3)) as! HDDatePickerCell
                    dateCell.state = inputViewState.activated
                    cell.state = inputViewState.disabled
                    }, failed: {_ in
                        cell.state = inputViewState.wrong
                })
                }, failed: { 
                    cell.state = inputViewState.wrong
            })
            break
        default:
            break
        }
        
    }
    //MARK: - private
    override func isEmpty() -> (Bool) {
        return self.emptyFlag
//        if let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as? HDBarcodeInputCell{
//            if cell.codeInputView.textField.text?.characters.count > 0 {
//                return false
//            }else{
//                return true
//            }
//        }else{
//            return false
//        }
    }
    //MARK: - tableviewDatasource
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dataArray.count
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray[section].count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellOriginal:HDFounctionBaseCell?
        switch (indexPath.section) * 100 + (indexPath.row) {
        case 0:
            cellOriginal = tableView.dequeueReusableCellWithIdentifier("HDBarcodeInputCell") as! HDBarcodeInputCell
            let cell = cellOriginal as! HDBarcodeInputCell
            cell.InputDelegate = self
            
        case 100,200:
            cellOriginal = tableView.dequeueReusableCellWithIdentifier("HDBarcodeInputCell") as! HDBarcodeInputCell
            let cell = cellOriginal as! HDBarcodeInputCell
            cell.InputDelegate = self
            
        case 101..<107:
            cellOriginal = tableView.dequeueReusableCellWithIdentifier("HDProductInfocell") as! HDProductInfocell
        case 300:
            cellOriginal = tableView.dequeueReusableCellWithIdentifier("HDDatePickerCell") as! HDDatePickerCell
            let cell = cellOriginal as! HDDatePickerCell
            cell.delegate = self
        case 301,302:
            cellOriginal = tableView.dequeueReusableCellWithIdentifier("HDNumberInputCell") as! HDNumberInputCell
            let cell = cellOriginal as! HDNumberInputCell
            cell.delegate = self
        default:
            cellOriginal = tableView.dequeueReusableCellWithIdentifier("HDProductInfocell") as! HDProductInfocell
        }
        if !self.reuseFlag {
            cellOriginal?.clearContent()
        }
        cellOriginal!.titleView.text = dataArray[indexPath.section][indexPath.row]
        return cellOriginal!
    }
    //MARK: - tableViewDelegate
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        switch section {
        case 2:
            return UIView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: 0.1))
        default:
            return nil
        }
    }
}


class infoView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init(){
        super.init(frame: CGRectZero)
    }
    var codeArr: [String]?
    var count: Int?
    var codeLine: Int?
    var codeHeight: CGFloat?
    lazy private var bgView:UIView = {
        let view = UIView(frame: CGRect(x: KscreenWidth / 2 - 140, y: KscreenHeight / 2 - 165, width: 280, height: 300))
        view.layer.cornerRadius = 10
        view.backgroundColor = UIColor.whiteColor()
        view.clipsToBounds = true
        return view
    }()
    lazy private var clearButton:UIButton = {
        let button = UIButton(frame: CGRect(x: 300 - 50, y: 10, width: 40, height: 40))
        button.setImage(UIImage(named: "Cancel"), forState: UIControlState.Normal)
        return button
    }()

    convenience init(dict: [String : String] , piPalletCode: String) {
        self.init()
        self.frame = CGRectMake(0, 0 , KscreenWidth, KscreenHeight)
        self.backgroundColor = UIColor.hexStringToUIColor("#666666", alpha: 0.4)
        self.addSubview(bgView)
        let blueView = UIView()
        self.bgView.addSubview(blueView)
        blueView.snp_makeConstraints { (make) in
            make.top.equalTo(self.bgView).offset(0)
            make.left.trailing.equalTo(self.bgView)
            make.height.equalTo(40)
        }
        let title = UILabel()
        title.text = "托盘查询"
        title.textColor = UIColor.whiteColor()
        title.font = UIFont.systemFontOfSize(20)
        self.bgView.addSubview(title)
        title.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.bgView)
            make.top.equalTo(self.bgView).offset(5)
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        blueView.backgroundColor = UIColor.hexStringToUIColor("#3F95FC")
        self.bgView.addSubview(clearButton)
        clearButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(self.bgView).offset(5)
            make.top.equalTo(self.bgView).offset(-5)
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
        clearButton.addTarget(self, action: #selector(infoView.clickClear(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        let stirngarr = ["托盘条码", "商品条码", "商品名称", "规格" , "件数" , "状态"]
        let codeArr = dict["poArticleCode"]?.componentsSeparatedByString(",")
        self.codeArr = codeArr
        self.count = self.codeArr?.count ?? 1
        self.codeLine = (self.count! - 1) / 2 + 1
        self.codeHeight = CGFloat((self.codeLine! - 1) * 24)
        self.bgView.height = 340 + 24 * CGFloat((self.count! - 1) / 2) + 20
        var codeString = ""
        for i in 0..<codeArr!.count{
            if i % 2 == 1 {
                codeString += codeArr![i] + "\n"
            }else{
                if i != (codeArr?.count)! - 1{
                    codeString += codeArr![i] + " / "
                }else{
                    codeString += codeArr![i]
                }
            }
        }
        for j in 0...5{
            self.addButtton(j, title: stirngarr[j])
        }
        for i in 0...5{
            switch i {
            case 0 :
                self.addLabel(i, text:piPalletCode)
                break
            case 1 :
                self.addLabel(i, text:codeString )
                break
            case 2 :
                self.addLabel(i, text: dict["poArticleName"]!)
                break
            case 3 :
                self.addLabel(i, text:dict["poSpec"]!)
                break
            case 4 :
                self.addLabel(i, text:dict["poQpc"]!)
                break
            case 5 :
                self.addLabel(i, text:dict["poStat"]!)
                break
            default :
                break
            }
        }
    }
    func clickClear(button : UIButton){
        self.removeFromSuperview()
    }
    func addButtton(order : Int , title: String){
        let button = UIButton()
        button.setTitle(title, forState: UIControlState.Normal)
        button.setImage(UIImage(named: "椭圆-灰" ), forState: UIControlState.Normal)
        self.bgView.addSubview(button)
        button.setTitleColor(UIColor.hexStringToUIColor("#333333"), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.boldSystemFontOfSize(17)
        switch order {
        case 0,1:
            button.snp_makeConstraints { (make) in
                make.top.equalTo(50 * order + 40)
                make.left.equalTo(self.bgView).offset(20)
                make.height.equalTo(26)
            }
            break
        case 2...5:
            button.snp_makeConstraints { (make) in
                make.top.equalTo(50 * order + 40 + Int(self.codeHeight!))
                make.left.equalTo(self.bgView).offset(20)
                make.height.equalTo(26)
            }
            break
        default:
            break
        }
        
    }
    func addLabel(order: Int , text: String){
        let label = UILabel()
        label.text = text
        label.tag = order
        label.numberOfLines = 10
        label.adjustsFontSizeToFitWidth = true
        label.textColor = UIColor.hexStringToUIColor("#66666")
        label.font = UIFont.systemFontOfSize(16)
        let sepView = UIView()
        sepView.backgroundColor = UIColor.hexStringToUIColor("#CCCCCC")
        self.bgView.addSubview(label)
        switch order {
        case 0:
            label.snp_makeConstraints { (make) in
                make.top.equalTo(order * 50 + 40 + 26)
                make.height.equalTo(24)
                make.left.equalTo(self.bgView).offset(35)
                make.trailing.equalTo(self.bgView).offset(10)
            }
            break
        case 1:
            label.snp_makeConstraints { (make) in
                make.top.equalTo(order * 50 + 40 + 26)
                make.height.equalTo(24 * self.codeLine!)
                make.left.equalTo(self.bgView).offset(35)
                make.trailing.equalTo(self.bgView).offset(10)
            }
            break
        case 2...5:
            label.snp_makeConstraints { (make) in
                make.top.equalTo(order * 50 + 66 + Int(self.codeHeight!))
                make.height.equalTo(24)
                make.left.equalTo(self.bgView).offset(35)
                make.trailing.equalTo(self.bgView).offset(10)
            }
            break
        default:
            break
        }
        self.bgView.addSubview(sepView)
        sepView.snp_makeConstraints { (make) in
            make.left.equalTo(self.bgView).offset(32)
            make.top.equalTo(label.snp_bottom).offset(-1)
            make.trailing.equalTo(self.bgView).offset(-15)
            make.height.equalTo(1)
        }
    }
}
